﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;

namespace TGVManagement.Models 
{
  /// <summary>
  /// Class resposility of Category
  /// </summary>
  public class CatResposility : ICatResposility
  {
    private DataContextDataContext dataContext = DataBase.GetDataContext();
    private Log4NetLogger logger = new Log4NetLogger();
    /// <summary>
    /// GetCategoryList
    /// </summary>
    /// <returns></returns> 
    public IList<CATEGORY> GetCategoryList()
    {
      logger.Info(Constants.START + "GetCategoryList");
      List<CATEGORY> lst_Cat = new List<CATEGORY>();
      try
      {
        var category = from cat in dataContext.CATEGORies
                       where cat.DEL_FLG.Equals(Constants.ZERO)
                       orderby cat.CAT_NAME
                       select cat;
        lst_Cat = category.ToList();
      }
      catch (Exception ex)
      {
        logger.Error(ex);
        throw;
      }

      logger.Info(Constants.END + "GetCategoryList");
      return lst_Cat;
    }
    /// <summary>
    ///  Add or update Category
    /// </summary>
    /// <param name="cat"></param>
    /// <param name="option"></param>
    /// <returns></returns>
    public bool Add_UpdCategory(CATEGORY cat, int option)
    {
      logger.Info(Constants.START + "Add_UpdCategory");
      bool bnlResult = false;
      try
      {
        //Add New
        if (option == 0)
        {
          dataContext.CATEGORies.InsertOnSubmit(cat);
          
        }
        else
        {
          var category = GetCategoryDetail(cat.CAT_CD);
          category.CAT_NAME = cat.CAT_NAME;
          category.ACCOUNT = cat.ACCOUNT;
          category.INPUT_DATE = cat.INPUT_DATE;
          category.INPUT_ID = cat.INPUT_ID;
          category.DEL_FLG = cat.DEL_FLG;

        }
        dataContext.SubmitChanges();
      }
      catch (Exception ex)
      {
        logger.Error(ex.Message);
        throw;
      }
      logger.Info(Constants.END + "Add_UpdCategory");
      return bnlResult;
    }
    /// <summary>
    /// Get category detail
    /// </summary>
    /// <param name="cat_Cd"></param>
    /// <returns></returns>
    public CATEGORY GetCategoryDetail(int cat_Cd)
    {
      logger.Info(Constants.START + "GetCategoryDetail");

      var category = (from c in dataContext.CATEGORies
                      where c.CAT_CD.Equals(cat_Cd)
                      select c).SingleOrDefault();
      
      logger.Info(Constants.END + "GetCategoryDetail");
      
      return category;
    }
    /// <summary>
    /// DelCategory
    /// </summary>
    /// <param name="cat_Cd"></param>
    /// <returns></returns>
    public bool DelCategory(int cat_Cd)
    {
      logger.Info(Constants.START + "DelCategory");

      var category = GetCategoryDetail(cat_Cd);
      category.DEL_FLG = char.Parse(Constants.ONE);
      dataContext.SubmitChanges();

      logger.Info(Constants.END + "DelCategory");
      
      return true;
    }
  }
}